package com.itheima.controller;


import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j//日志
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    //查询全部部门
    @GetMapping
    public Result listDept(){
        log.info("查询全部部门");
        List<Dept> listdept = deptService.listdept();

        return Result.success(listdept);
    }

    /**
     * 删除部门
     * @PathVariable 获取路径传来的值
     */
    @DeleteMapping("{id}")
    public Result deleteDept(@PathVariable Integer id){
        log.info("删除部门的id为" + id);
        deptService.deleteDept(id);
        return Result.success();
    }

    /**
     * 新增部门
     * @RequestBody 把前段传来的json的值存入实体类
     */
    @PostMapping
    public Result insertDept(@RequestBody Dept dept){
        log.info("新增部门数据为"+dept);
        deptService.insertDept(dept);
        return Result.success();
    }

    /**
     * 根据ID查询部门信息
     */
    @GetMapping("/{id}")
    public Result listByid(@PathVariable("id") Integer id){
        log.info("查询ID为:{}",id);
        Dept dept =deptService.listByid(id);
        log.info("查询的信息为",dept.getName());
        return Result.success(dept);

    }


    /**
     * 修改部门信息
     * @RequestBody主要用来接收前端传递给后端的json字符串中的数据的
     */
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改数据:{}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }



}
